﻿Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.UI.Styles

Namespace Global.Runa.UI.Controls

    Public Class TextLine
        Inherits ContentControl

        Protected _Progress As Single
        Protected _Text As String
        Protected _TextStyle As TextStyle
        Protected Friend DrawClip As Rectangle
        Protected TextClip As Rectangle

        Public Shadows Property Content() As TextContainerBase

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
        End Sub

        Private Sub New(ByVal Name As String)
            MyBase.New(Name)
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase, ByVal textStyle As TextStyle)
            MyBase.New(Name, Parent)
            Me._TextStyle = textStyle
            Me.Size = textStyle.Font.MeasureString(textStyle.Font.Characters.Item(0).ToString)
            MyBase.IsUseDrawBounds = False
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase, ByVal [Text] As String, ByVal textStyle As TextStyle)
            MyBase.New(Name, Parent)
            Me._TextStyle = textStyle
            Me.Text = [Text]
            Me.Size = textStyle.Font.MeasureString([Text])
            MyBase.IsUseDrawBounds = False
        End Sub

        Public Overrides Sub Draw(ByVal SpriteBatch As SpriteBatch)
            If MyBase.Visible AndAlso Me.Content IsNot Nothing Then
                If Not MyBase.Effect Is Nothing Then
                    SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Nothing, Nothing, Nothing, MyBase.Effect.Effect)
                Else
                    SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend)
                End If
                Dim c = Color.White
                If Me.TextStyle IsNot Nothing AndAlso Me.TextStyle.TextColor <> Nothing Then
                    c = Me.TextStyle.TextColor
                End If
                If Not MyBase.Background Is Nothing Then
                    SpriteBatch.Draw(MyBase.Background.Texture, Me.DrawBounds, Color.White * MyBase.Opacity)
                End If
                If Not Me.Content.TextTexture.Texture Is Nothing Then
                    SpriteBatch.Draw(Me.Content.TextTexture.Texture, Me.DrawClip, New Nullable(Of Rectangle)(Me.TextClip), c * MyBase.Opacity, Me.AbsoluteRotation, MyBase.Orgin, MyBase.SpriteEffect, 0.5!)
                End If
                SpriteBatch.End()
            End If
            MyBase.Draw(SpriteBatch)
        End Sub

        Protected Overrides Sub OnBoundsChange()
            MyBase.OnBoundsChange()
            If ((Not Me.Content Is Nothing) AndAlso (Not Me.Content.TextTexture Is Nothing)) Then
                Me.DrawClip = New Rectangle(Me.DrawBounds.X, Me.DrawBounds.Y, CInt((Me.DrawBounds.Width * Me._Progress)), Me.DrawBounds.Height)
                Me.TextClip = New Rectangle(0, 0, CInt((Me.Content.TextTexture.Texture.Width * Me._Progress)), Me.Content.TextTexture.Texture.Height)
            End If
            'If Me.Content IsNot Nothing AndAlso Me.Content.TextTexture IsNot Nothing Then
            '    Me.DrawClip = New Rectangle(Me.Location.X, Me.Location.Y, CInt(Me.Size.X * Me._Progress), Me.DrawBounds.Height)
            '    Me.TextClip = New Rectangle(0, 0, CInt((Me.Content.TextTexture.Texture.Width * Me._Progress)), Me.Content.TextTexture.Texture.Height)
            'End If
        End Sub

        Public Overrides Sub Update(ByVal gameTime As GameTime)
            MyBase.Update(gameTime)
        End Sub

        Public Overridable Property Progress() As Single
            Get
                Return Me._Progress
            End Get
            Set(ByVal value As Single)
                If (((Me._Progress <> value) AndAlso (value >= 0.0!)) AndAlso (value <= 1.0!)) Then
                    Me._Progress = value
                    If ((Not Me.Content Is Nothing) AndAlso (Not Me.Content.TextTexture Is Nothing)) Then
                        Me.DrawClip = New Rectangle(Me.DrawBounds.X, Me.DrawBounds.Y, CInt((Me.DrawBounds.Width * value)), Me.DrawBounds.Height)
                        Me.TextClip = New Rectangle(0, 0, CInt((Me.Content.TextTexture.Texture.Width * value)), Me.Content.TextTexture.Texture.Height)
                    End If
                End If
            End Set
        End Property

        Public Overrides Property Size() As Vector2
            Get
                Return MyBase.Size
            End Get
            Set(ByVal value As Vector2)
                If (MyBase._Size <> value) Then
                    MyBase.Size = value
                    If ((Not Me._Text Is Nothing) AndAlso (Not MyBase._Animations Is Nothing)) Then
                        If (MyBase._Animations.Count > 0) Then
                            If MyBase._Animations.Exists(Function(anim) anim.TargetPropertyName = "Progress") Then
                                Me.Content = New TextContainerBase(MyBase.Game, Me._Text, MyBase._Size, Me._TextStyle)
                            End If
                        Else
                            Me.Content = New TextContainerBase(MyBase.Game, Me._Text, MyBase._Size, Me._TextStyle)
                        End If
                    End If
                End If
            End Set
        End Property

        Public Overridable Property [Text]() As String
            Get
                Return Me._Text
            End Get
            Set(ByVal value As String)
                If (Me._Text <> value) Then
                    Me._Text = value
                    If (Not Me._Text Is Nothing) Then
                        Me.Content = New TextContainerBase(MyBase.Game, value, Me.Size, Me._TextStyle)
                        If (MyBase._Size = Vector2.Zero) Then
                            Me.Size = Me._TextStyle.Font.MeasureString(Me.Text)
                        End If
                    Else
                        Me.Content = Nothing
                    End If
                    If (Me.Progress = 0.0!) Then
                        Me.Progress = 1.0!
                    End If
                End If
            End Set
        End Property

        Public Property TextStyle() As TextStyle
            Get
                Return Me._TextStyle
            End Get
            Set(ByVal value As TextStyle)
                If (Not Me._TextStyle Is value) Then
                    Me._TextStyle = value
                End If
            End Set
        End Property

    End Class

End Namespace